#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    int coefficient; //系数
    int exponent;    //指数
    struct Node *next;
} Node, *LinkList;

void initList(LinkList *head)
{
    if ((*head = (LinkList)malloc(sizeof(Node))) == NULL)
        exit(-1);
    (*head)->next = NULL;
}
void creatList(LinkList head)
{
    LinkList p, tail = head;
    int count;
    scanf("%d", &count);
    while (count-- > 0)
    {
        p = (LinkList)malloc(sizeof(Node));
        scanf("%d%d", &p->coefficient, &p->exponent);
        tail->next = p;
        tail = p;
    }
    tail->next = NULL;
}
void printList(LinkList head)
{
    LinkList p = head->next;
    while (p)
    {
        if (p == head->next)
            printf("%d %d", p->coefficient, p->exponent);
        else
            printf(" %d %d", p->coefficient, p->exponent);
        p = p->next;
    }
}

void addition(LinkList list1, LinkList list2)
{
    LinkList result, p = list1->next, q = list2->next, tail, newNode;
    initList(&result);
    tail = result;
    while (p && q)
    {
        int coefficient1, coefficient2, exponent1, exponent2;
        newNode = (LinkList)malloc(sizeof(Node));

        // 当p的指数大于q的指数，将节点p复制给newNode，将newNode移动到result的末尾，p往后移动，q不动
        if (p->exponent > q->exponent)
        {
            newNode->coefficient = p->coefficient;
            newNode->exponent = p->exponent;
            newNode->next = NULL;
            tail->next = newNode;
            tail = newNode;
            p = p->next;
        }
        else if (p->exponent < q->exponent)
        {
            // 当p的指数小于q的指数，将节点q复制给newNode，将newNode移动到result的末尾，q往后移动，p不动
            newNode->coefficient = q->coefficient;
            newNode->exponent = q->exponent;
            newNode->next = NULL;
            tail->next = newNode;
            tail = newNode;
            q = q->next;
        }
        else
        {
            // 当p和q的指数相等，直接将两个的系数相加。如果相加后的系数为0，则将p，q直接移动到下一位
            if (p->coefficient + q->coefficient == 0)
            {
                p = p->next;
                q = q->next;
                free(newNode);
            }
            else
            {
                newNode->coefficient = p->coefficient + q->coefficient;
                newNode->exponent = p->exponent;
                newNode->next = NULL;
                tail->next = newNode;
                tail = newNode;
                p = p->next;
                q = q->next;
            }
        }
    }
    // 如果p或q还有剩余节点，直接接到result的末尾
    if (!p)
    {
        tail->next = q;
    }
    if (!q)
    {
        tail->next = p;
    }

    if (!result->next)
    {
        free(result);
        printf("0 0");
    }
    else
    {
        printList(result);
    }
}

void multiplication(LinkList list1, LinkList list2)
{
    LinkList result, p = list1->next, q , tail, newNode;
    initList(&result);
    tail = result;
    while (p)
    {
        q = list2->next;
        while (q)
        {
            // 创建新建的newNode
            newNode = (LinkList)malloc(sizeof(Node));
            newNode->coefficient = p->coefficient * q->coefficient;
            newNode->exponent = p->exponent + q->exponent;
            newNode->next = NULL;

            LinkList temp = result;
            // 找到合适的插入位置
            while (temp->next && newNode->exponent < temp->next->exponent)
            {
                temp = temp->next;
            }
            // 插入或者合并同类项
            if (temp->next == NULL || newNode->exponent > temp->next->exponent)
            {
                newNode->next = temp->next;
                temp->next = newNode;
            }
            else if (newNode->exponent == temp->next->exponent)
            {
                temp->next->coefficient += newNode->coefficient;
                // 如果合并同类项后系数为0，得删除这个节点
                if(!temp->next->coefficient){
                    LinkList deleteNode=temp->next;
                    temp->next=deleteNode->next;
                    free(deleteNode);
                }
                free(newNode);
            }
            q = q->next;
        }
        p = p->next;
    }
    if (!result->next)
    {
        free(result);
        printf("0 0");
    }
    else
    {
        printList(result);
    }
}
int main()
{
    LinkList list1, list2;
    initList(&list1);
    initList(&list2);
    creatList(list1);
    creatList(list2);
    multiplication(list1, list2);
    printf("\n");
    addition(list1, list2);
    return 0;
}